package com.sojson.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sojson.config.exception.TransException;
import com.sojson.dao.GoodCateDao;
import com.sojson.entity.bo.Select;
import com.sojson.entity.po.GoodCate;
import com.sojson.entity.po.Goods;
import com.sojson.service.GoodCateService;
import com.sojson.service.GoodsService;
import com.sojson.util.file.FileDepotUtil;
import com.sojson.util.file.constant.ConstantFileFieldName;
import com.sojson.util.file.enums.EFileTable;

/**
 * 商品分类表Service实现类
 * 
 * @author liu
 * @date 2021-03-16
 */
@Service
public class GoodCateServiceImpl implements GoodCateService {

    @Autowired
    private GoodsService goodsService;
    @Resource
    private GoodCateDao goodCateDaoImpl;

    /**
     * 单条插入
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    @Override
    public int insert(GoodCate goodCate) {
        int insert = goodCateDaoImpl.insert(goodCate);

        if (insert > 0) {
            FileDepotUtil.insertSync(EFileTable.GOOD_CATE, ConstantFileFieldName.THUMB, goodCate.getThumb());
        }
        return insert;
    }

    /**
     * 批量插入
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    @Override
    public int insertList(GoodCate goodCate) {
        int insertList = goodCateDaoImpl.insertList(goodCate.getGoodCates());
        return insertList;
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByDto(GoodCate goodCate) {
        int deleteByDto = goodCateDaoImpl.deleteByDto(goodCate);
        return deleteByDto;
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    @Override
    public int countAll() {
        int countAll = goodCateDaoImpl.countAll();
        return countAll;
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    @Override
    public List<GoodCate> findAll() {
        List<GoodCate> findAll = goodCateDaoImpl.listAll();
        return findAll;
    }

    /**
     * 根据对象统计数量
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    @Override
    public int countByDto(GoodCate goodCate) {
        int countByDto = goodCateDaoImpl.countByDto(goodCate);
        return countByDto;
    }

    /**
     * 根据对象查询
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    @Override
    public List<GoodCate> findByDto(GoodCate goodCate) {
        List<GoodCate> findByDto = goodCateDaoImpl.listByDto(goodCate);
        return findByDto;
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteById(Integer id) {
        Goods goods = new Goods();
        goods.setCateId(id);
        int countByDto = goodsService.countByDto(goods);
        if (countByDto > 0) {
            throw new TransException("商品分类正在使用,请先删除商品！");
        }
        int deleteById = goodCateDaoImpl.deleteById(id);
        return deleteById;
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByIdList(ArrayList<Integer> ids) {
        List<GoodCate> findByIdList = goodCateDaoImpl.findByIdList(ids);

        int deleteByIdList = goodCateDaoImpl.deleteByIdList(ids);

        if (deleteByIdList > 0) {
            List<String> orderNoList = findByIdList.stream().map(GoodCate::getThumb).collect(Collectors.toList());
            FileDepotUtil.deleteListSync(orderNoList);
        }
        return deleteByIdList;
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    @Override
    public int updateById(GoodCate goodCate) {
        int updateById = goodCateDaoImpl.updateById(goodCate);
        return updateById;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    @Override
    public int updateStaticByIdList(GoodCate goodCate) {
        int updateStaticByIdList = goodCateDaoImpl.updateStaticByIdList(goodCate);
        return updateStaticByIdList;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    @Override
    public int updateChangeByIdList(GoodCate goodCate) {
        int updateChangeByIdList = goodCateDaoImpl.updateChangeByIdList(goodCate.getGoodCates());
        return updateChangeByIdList;
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    @Override
    public int updatePartialById(GoodCate goodCate) {
        GoodCate findById = goodCateDaoImpl.findById(goodCate.getId());

        int updatePartialById = goodCateDaoImpl.updatePartialById(goodCate);

        if (updatePartialById > 0) {
            FileDepotUtil.updateSync(EFileTable.GOOD_CATE.getCode(), ConstantFileFieldName.THUMB, goodCate.getThumb(),
                findById.getThumb());
        }
        return updatePartialById;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    @Override
    public int updateStaticPartialByIdList(GoodCate goodCate) {
        int updateStaticPartialByIdList = goodCateDaoImpl.updateStaticPartialByIdList(goodCate);
        return updateStaticPartialByIdList;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    @Override
    public int updateChangePartialByIdList(GoodCate goodCate) {
        int updateChangePartialByIdList = goodCateDaoImpl.updateChangePartialByIdList(goodCate.getGoodCates());
        return updateChangePartialByIdList;
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public GoodCate findById(Integer id) {
        GoodCate findById = goodCateDaoImpl.findById(id);
        return findById;
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    @Override
    public ArrayList<GoodCate> findByIdList(ArrayList<Integer> ids) {
        ArrayList<GoodCate> findByIdList = goodCateDaoImpl.findByIdList(ids);
        return findByIdList;
    }

    /**
     * 通过对象查询id列表
     * 
     * @param goodCate
     * @return
     * @throws SQLException
     */
    @Override
    public ArrayList<Integer> findIdByDto(GoodCate goodCate) {
        ArrayList<Integer> findIdByDto = goodCateDaoImpl.findIdByDto(goodCate);
        return findIdByDto;
    }

    /**
     * 清空数据(物理清空)
     * 
     * @throws SQLException
     */
    @Override
    public void clear() {
        goodCateDaoImpl.clear();
    }

    /**
     * 根据对象查询
     * 
     * @param goodCate
     * @return
     */
    @Override
    public List<Select> findSelect(GoodCate goodCate) {
        return goodCateDaoImpl.findSelect(goodCate);
    }

}